package com.mall.db.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.db.mapper.UmsResourceMapper;
import com.mall.domain.UmsResource;
import com.mall.interface1.api.UmsResourceApi;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@DubboService
public class UmsResourceApiImpl implements UmsResourceApi {
    @Autowired
    private UmsResourceMapper mapper;
    @Override
    public void deleteIds(List<Long> ids) {
        mapper.deleteBatchIds(ids);
    }
    @Override
    public List<UmsResource> selectCateId(Integer id) {
        LambdaQueryWrapper<UmsResource> wq = new LambdaQueryWrapper<>();
        wq.in(UmsResource::getCategoryId,id);
        return mapper.selectList(wq);
    }

    @Override
    public void save(UmsResource umsResource) {
        if(umsResource.getCreateTime()==null){
            umsResource.setCreateTime(LocalDateTime.now());
        }
        mapper.insert(umsResource);
    }

    @Override
    public void deleteId(Integer id) {
        mapper.deleteById(id);
    }

    @Override
    public IPage<UmsResource> listQuery(Integer categoryId, String nameKeyword, Integer pageNum, Integer pageSize, String urlKeyword) {
        IPage<UmsResource> page=new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<UmsResource> qw = new LambdaQueryWrapper<>();
        qw.eq(categoryId!=null,UmsResource::getCategoryId,categoryId);
        qw.like(nameKeyword!=null,UmsResource::getName,nameKeyword);
        qw.like(urlKeyword!=null,UmsResource::getUrl,urlKeyword);
        return mapper.selectPage(page,qw);
    }

    @Override
    public List<UmsResource> listAll() {
        return mapper.selectList(null);
    }

    @Override
    public void update(UmsResource umsResource) {
        mapper.updateById(umsResource);
    }

    @Override
    public UmsResource queryId(Integer id) {
        return mapper.selectById(id);
    }

    @Override
    public List<UmsResource> findByIds(List<Long> resourceIds) {
        return mapper.selectBatchIds(resourceIds);
    }
}

